const api = require('../../api/index')
const app = getApp()
const Second = require("../../utils/time.js")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    animation: '',
    playing: true,
    ids: "",
    songdetalist: [],
    playicon: "/asstes/stop.png",
    lyriclist: [],
    songlistall: [],
    height: "0",
    songUrl: `https://music.163.com/song/media/outer/url?id=`,
    curtime: 0,
    durtime: 0,
    jindu: 0,
  },
  // 音乐播放
  play() {
    let url = this.data.songUrl + this.data.ids + ".mp3"
    app.globalData.BackgroundAudioManager.src = url
    app.globalData.BackgroundAudioManager.title = this.data.songdetalist.name

    app.globalData.BackgroundAudioManager.onTimeUpdate(() => {
      let curTime = app.globalData.BackgroundAudioManager.currentTime
      let durTime = app.globalData.BackgroundAudioManager.duration
      let jinDu = curTime / durTime * 100
      this.setData({
        curtime: Second(curTime),
        durtime: Second(durTime),
        jindu: jinDu,
      })
    })
  },

  // 音乐播放和暂停
  songplaying() {
    let play = this.data.playing
    if (play) {
      app.globalData.BackgroundAudioManager.pause()
      this.setData({
        playing: !play,
        playicon: "/asstes/play-gl.png",
      })
    } else {
      app.globalData.BackgroundAudioManager.play()
      this.setData({
        playing: !play,
        playicon: "/asstes/stop.png",
      })
    }
  },
  // 音乐列表显示或者消失
  listshow() {
    this.setData({
      height: "-900"
    })
  },
  listhide() {
    this.setData({
      height: "0"
    })
  },
  // 上一首歌曲
  Previoussong() {
    let that = this
    app.globalData.curIndex--;
    if (app.globalData.curIndex < 0) {
      app.globalData.curIndex = 0
      return
    }
    // 获取歌词
    this.getsong(app.globalData.list[app.globalData.curIndex].id)
    this.getlyricc(app.globalData.list[app.globalData.curIndex].id)
  },
  //下一首歌曲
  Nextsong() {
    let that = this
    app.globalData.curIndex++;
    if (app.globalData.curIndex >= app.globalData.list.length) {
      app.globalData.curIndex = app.globalData.list.length
      return
    }

    this.getsong(app.globalData.list[app.globalData.curIndex].id)
    this.getlyricc(app.globalData.list[app.globalData.curIndex].id)

  },
  // 收藏歌曲
  Collectsongs() {

  },
  // 请求歌曲歌词
  getlyricc(id) {
    // 获取歌词
    api.getlyric({
        id: id
      })
      .then(
        content => {
          let {
            code,
            lrc: {
              lyric
            }
          } = content;
          if (code == 200) {
            // 定义空列表装歌词
            let lrclist = []
            const lyriclist = lyric.split("\n")
            const re = /\[\d{2}:\d{2}\.\d{2,3}\]/
            // 遍历列表
            lyriclist.forEach(item => {
              if (item) {
                // 获取时间
                let itemdate = item.match(re)
                // console.log(itemdate);
                // 判断剔除掉空时间
                if (itemdate) {
                  itemdate = itemdate[0]
                  //整理时间
                  itemdate = itemdate.slice(1, -1)
                  const timelist = itemdate.split(":")
                  const time0 = timelist[0]
                  const time1 = timelist[1]
                  const time = parseFloat(time0) * 60 + parseFloat(time1)
                  // 找歌词,把符合时间的去除掉
                  const lrcstr = item.replace(re, "")
                  // 给歌曲与时间进行匹配整合
                  lrclist.push([time, lrcstr])
                }
              }

            });
            // 赋值
            this.setData({
              lyriclist: lrclist
            })
          }
          // console.log(this.data.lyriclist);
        }
      )
  },

  getsong(id) {
    // 获取歌曲详情
    api.getsongdetail({
        ids: id
      })
      .then(
        content => {
          let {
            code,
            songs
          } = content;
          if (code == 200) {
            // 赋值
            this.setData({
              songdetalist: songs[0],
            })
            this.play()
          }
        }
      )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(app.globalData.list);

    let str = options.ids
    if (str) {
      this.setData({
        ids: str,
        songlistall: app.globalData.list
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getsong(this.data.ids)
    this.getlyricc(this.data.ids)


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})